package com.xxx.baseapplication.binds

import android.content.Intent
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.xxx.baseapplication.ImageShowActivity
import com.xxx.baseapplication.util.Constants
import com.xxx.baseapplication.vo.PositionImage
import com.xxx.mylibrary.binds.displayWithUrl
import com.xxx.mylibrary.binds.getActivity
import com.xxx.mylibrary.binds.screenHeight
import com.xxx.mylibrary.binds.screenWidth
import com.xxx.mylibrary.widget.TouchImageView


/**
 * 显示图片
 */
@BindingAdapter(value = ["showImage"])
fun bindImage(iv: ImageView, showImage: PositionImage?) {
    showImage?.run {
        val lp = iv.layoutParams as ViewGroup.MarginLayoutParams
        val parentWidth = iv.context.resources.displayMetrics.widthPixels
        val scale = parentWidth / clientWidth
        lp.width = (width * scale).toInt()
        lp.height = (height * scale).toInt()
        lp.leftMargin = (x * scale).toInt()
        lp.topMargin = (y * scale).toInt()
        iv.layoutParams = lp
        if (TextUtils.isEmpty(url)) return@run
        iv.displayOverride(url)
        iv.postDelayed({
            val activity = iv.getActivity()
            activity?.let {
                val pair: Pair<View, String> = Pair(iv, "image")
                val option =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(
                        it,
                        pair
                    )
                val intent = Intent(it, ImageShowActivity::class.java)
                Constants.sharedUrl = url
                it.startActivityForResult(intent, 1, option.toBundle())
            }
        }, 100)
    }
}

fun ImageView.displayOverride(
    url: String
) {
    Glide.with(this)
        .asBitmap()
        .load(url)
        .override(context.screenWidth(), context.screenHeight())
        .into(this)
}

fun TouchImageView.displayCenterInside(url: String?, w: Float, h: Float) {
    Glide.with(this)
            .asDrawable()
            .fitCenter()
            .load(url)
            .into(object : SimpleTarget<Drawable>() {
                override fun onResourceReady(
                        resource: Drawable,
                        transition: Transition<in Drawable>?
                ) {
                    setImageDrawableToWith(resource, w, h)
                    if (resource is GifDrawable) {
                        resource.setVisible(true, true)
                        resource.start()
                    }

                }
            })
}

@BindingAdapter(value = ["url", "radius"])
fun bindImage(iv: ImageView, url: String?, radius: Int?) {
    if (url != null) {
        iv.displayWithUrl(url, (radius ?: 1).toFloat())
    }
}







